package com.util;

import org.springframework.stereotype.Component;

@Component
public class EncryUtil {
	//人员资料 密码  des加密默认key
	private final static String key = "96878265";
	
	/**
	 * 获取加密工具类DesUtil
	 * @return
	 * @throws Exception
	 */
	private static DesUtil getDesUtil() throws Exception{
		return new DesUtil(key);
	}
	
	/**
	 * 给密码加盐
	 * @param password 密码
	 * @param mobile 手机号
	 * @return
	 */
	private static String setPasswordSalt(String password,String mobile){
		return mobile+"{"+password+"}";
	}
	
	/**
	 * 获取密码
	 * @param password
	 * @return
	 */
	private static String removePasswordSalt(String password){
		return password.substring(password.indexOf("{")+1, password.length()-1);
	}
	/**
	 * 对密码进行加密
	 * @param password 密码
	 * @param mobile   手机号
	 * @return 加密后的密码
	 * @throws Exception
	 */
	public static String encryptPassword(String password,String mobile){
		try {
			return getDesUtil().encrypt(setPasswordSalt(password,mobile));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return password;
	}
	
	/**
	 * 对密码进行解密
	 * @param password 加密的密码
	 * @return 明文密码
	 * @throws Exception
	 */
	public  static String decryptPassword(String password){
		try {
			String pass = getDesUtil().decrypt(password);
			System.out.println(pass);
			return removePasswordSalt(pass);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return password;
	}

}
